package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCompanyChargeMapper;
import org.linlinjava.litemall.db.domain.LitemallCompanyCharge;
import org.linlinjava.litemall.db.domain.LitemallCompanyChargeExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallCompanyChargeService {
    @Resource
    private LitemallCompanyChargeMapper historyMapper;

    public List<LitemallCompanyCharge> queryIndex() {
        LitemallCompanyChargeExample example = new LitemallCompanyChargeExample();
        example.or().andDeletedEqualTo(false);
        return historyMapper.selectByExample(example);
    }

    public List<LitemallCompanyCharge> querySelective(String name, String content, Integer page, Integer limit, String sort, String order) {
        LitemallCompanyChargeExample example = new LitemallCompanyChargeExample();
        LitemallCompanyChargeExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return historyMapper.selectByExample(example);
    }

    public int updateById(LitemallCompanyCharge charge) {
        charge.setUpdateTime(LocalDateTime.now());
        return historyMapper.updateByPrimaryKeySelective(charge);
    }

    public void deleteById(Integer id) {
        historyMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallCompanyCharge charge) {
        charge.setAddTime(LocalDateTime.now());
        charge.setUpdateTime(LocalDateTime.now());
        historyMapper.insertSelective(charge);
    }

    public LitemallCompanyCharge findById(Integer id) {
        return historyMapper.selectByPrimaryKey(id);
    }
}
